package com.excel;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.julis.R;
import com.julis.bean.ObuTrade_Info;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExcelActivity extends AppCompatActivity {
    @BindView(R.id.out_excel)
    Button outExcel;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    private ArrayList<ArrayList<String>> recordList;
    private List<ObuTrade_Info> obuTradeInfos;
    private static String[] title = {"编号", "时间", "SN号", "OBU MAC号", "结果", "检测时长", "备注"};
    private File file;
    private String fileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_excel_);
        ButterKnife.bind(this);
        //模拟数据集合
        obuTradeInfos = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            obuTradeInfos.add(new ObuTrade_Info("小红" + i, "女", "女", "12", "1" + i, "一班", "一班"));
            obuTradeInfos.add(new ObuTrade_Info("小明" + i, "女", "男", "14", "2" + i, "二班", "二班"));
        }
        outExcel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                out_excel();
            }
        });
    }

    /**
     * 导出excel
     *
     * @param
     */
    public void out_excel() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH_mm_ss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        file = new File(getSDPath() + "/Record");
        makeDir(file);

        ExcelUtils.initExcel(file.toString() + "/交易测试记录" + str + ".xls", title, this);
        fileName = getSDPath() + "/Record/交易测试记录" + str + ".xls";
        ExcelUtils.writeObjListToExcel(getRecordData(), fileName, this);
    }

    /**
     * 将数据集合 转化成ArrayList<ArrayList<String>>
     *
     * @return
     */
    private ArrayList<ArrayList<String>> getRecordData() {
        recordList = new ArrayList<>();
        for (int i = 0; i < obuTradeInfos.size(); i++) {
            ObuTrade_Info obuTradeInfo = obuTradeInfos.get(i);
            ArrayList<String> beanList = new ArrayList<String>();
            beanList.add(obuTradeInfo.getIndex());
            beanList.add(obuTradeInfo.getChecktime());
            beanList.add(obuTradeInfo.getSn());
            beanList.add(obuTradeInfo.getMac());
            beanList.add(obuTradeInfo.getResults());
            beanList.add(obuTradeInfo.getTimelong());
            beanList.add(obuTradeInfo.getRemarks());
            recordList.add(beanList);
        }
        return recordList;
    }

    private String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
        }
        String dir = sdDir.toString();
        return dir;
    }

    public void makeDir(File dir) {
        if (!dir.getParentFile().exists()) {
            makeDir(dir.getParentFile());
        }
        dir.mkdir();
    }


}
